package com.tjshop.android;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.tjshop.android.constants.UrlConfigs;
import com.tjshop.android.db.userAccess;
import com.tjshop.android.mcv.GlobeVars;
import com.tjshop.android.model.userInfo;
import com.tjshop.android.util.HttpUtil;
import com.tjshop.android.util.WaitProgressDialog;
import org.json.JSONException;
import org.json.JSONObject;

public class RegistActivity extends BaseActivity {

	private Context mContext;
	private WaitProgressDialog mProgressDialog;

	EditText input_username, input_pass, input_email, input_tel;
	Button regist;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_regist);
		mContext = this;
		setTheme(R.style.Theme_Sherlock);
		updateTextViewBar("注册", "登陆", new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		mProgressDialog = new WaitProgressDialog(this);

		input_username = (EditText) findViewById(R.id.account);
		input_email = (EditText) findViewById(R.id.email);
		input_pass = (EditText) findViewById(R.id.pass);
		input_tel = (EditText) findViewById(R.id.tel);
		regist = (Button) findViewById(R.id.regist);

		regist.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				String username = input_username.getText().toString();
				if (TextUtils.isEmpty(username)) {
					Toast.makeText(getApplicationContext(), "请输入用户名",
							Toast.LENGTH_SHORT).show();
					input_username.requestFocus();
					return;
				}
				// 验证邮箱
				String email = input_email.getText().toString();
				if (TextUtils.isEmpty(email)) {
					Toast.makeText(getApplicationContext(), "请输入常用邮箱",
							Toast.LENGTH_SHORT).show();
					input_email.requestFocus();
					return;
				}
				Pattern pattern = Pattern
						.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");

				Matcher matcher = pattern.matcher(email);
				if (!matcher.matches()) {
					Toast.makeText(getApplicationContext(), "输入的邮箱格式有误，请重新输入",
							Toast.LENGTH_SHORT).show();
					input_email.setText("");
					input_email.requestFocus();
					return;
				}
				// 验证密码
				String pass = input_pass.getText().toString();
				if (TextUtils.isEmpty(pass)) {
					Toast.makeText(getApplicationContext(), "请输入登陆密码",
							Toast.LENGTH_SHORT).show();
					input_pass.requestFocus();
					return;
				}
				if (pass.length() < 6) {
					Toast.makeText(getApplicationContext(), "密码不能低于六位",
							Toast.LENGTH_SHORT).show();
					input_pass.setText("");
					input_pass.requestFocus();
					return;
				}
				// 验证手机号
				String tel = input_tel.getText().toString();
				if (TextUtils.isEmpty(tel)) {
					Toast.makeText(getApplicationContext(), "请输入手机号",
							Toast.LENGTH_SHORT).show();
					input_tel.requestFocus();
					return;
				}
				pattern = Pattern
						.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");

				matcher = pattern.matcher(tel);
				if (!matcher.matches()) {
					Toast.makeText(getApplicationContext(), "输入的手机号格式有误，请重新输入",
							Toast.LENGTH_SHORT).show();
					input_tel.setText("");
					input_tel.requestFocus();
					return;
				}
				regist(username, pass, email, tel);
			}
		});

	}

	private void regist(final String username, final String pass,
			final String email, final String tel) {
		mProgressDialog.show("注册中", true, false);

		StringRequest stringRequest = new StringRequest(Method.GET,
				UrlConfigs.URL_REGIST + "?userid=" + username + "&pwd=" + pass
						+ "&email=" + email + "&tel=" + tel,
				new Response.Listener<String>() {
					@Override
					public void onResponse(String response) {
						JSONObject jsonU;
						try {
							jsonU = new JSONObject(response);
							if (jsonU.getInt("r") == 0) {
								
								Message message = new Message();
								message.what = 2;
								mHandler.sendMessage(message);
							} else {
								Message message = new Message();
								message.what = 3;
								message.obj = jsonU.getString("m");
								mHandler.sendMessage(message);
							}
						} catch (JSONException e) {
							Message message = new Message();
							message.what = 1;
							message.obj = "服务器返回异常！";
							mHandler.sendMessage(message);
							e.printStackTrace();
						}

					}
				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
						Message message = new Message();
						message.what = 1;
						mHandler.sendMessage(message);

					}

				});

		mQueue.add(stringRequest);
	}

	@Override
	public boolean handleMessage(Message message) {
		switch (message.what) {
		case 1: // 通信异常
			mProgressDialog.dismiss();
			Toast.makeText(mContext, "服务器忙，请稍后再试！", Toast.LENGTH_SHORT).show();
			break;
		case 2: // 注册成功
			mProgressDialog.dismiss();
			Toast.makeText(mContext, "注册成功！", Toast.LENGTH_SHORT).show();
//			GlobeVars.isLogin = true;
//			GlobeVars.userId = input_username.getText().toString();
//			GlobeVars.pass = input_pass.getText().toString();
			finish();
			break;
		case 3: // 注册失败
			mProgressDialog.dismiss();
			Toast.makeText(mContext, (String) (message.obj), Toast.LENGTH_SHORT)
					.show();
			break;
		default:
			break;
		}
		return false;
	}
}
